package com.jiangyg.mall.coupon.controller;

import com.jiangyg.mall.core.support.restful.Pagination;
import com.jiangyg.mall.core.support.restful.Result;
import com.jiangyg.mall.coupon.dto.CouponSpuDTO;
import com.jiangyg.mall.coupon.service.CouponSpuService;
import com.jiangyg.mall.coupon.vo.CouponSpuVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 类描述：优惠券与产品关联控制器
 *
 * @author jiangyg
 * @version email@jiangyg.com
 * @date 2021-10-18 21:33:06
 */
@RestController
@RequestMapping("/coupon/spu")
public class CouponSpuController {

    /**
     * 优惠券与产品关联接口
     */
    private final CouponSpuService couponSpuService;

    @Autowired
    public CouponSpuController(CouponSpuService couponSpuService) {
        this.couponSpuService = couponSpuService;
    }

    /**
     * 功能描述：优惠券与产品关联分页查询
     *
     * @param request    请求
     * @param pagination 分页参数
     * @return 返回分页优惠券与产品关联
     */
    @ResponseBody
    @PostMapping("/select")
    public Result<Pagination<CouponSpuVO>> select(HttpServletRequest request, @RequestBody Pagination<CouponSpuVO> pagination) throws Exception {
        return Result.ok(couponSpuService.selectPagination(pagination));
    }

    /**
     * 功能描述：保存或者修改优惠券与产品关联
     *
     * @param request 请求
     * @param dto     优惠券与产品关联
     * @return 结果
     */
    @ResponseBody
    @PostMapping("/save")
    public Result<Object> save(HttpServletRequest request, @RequestBody @Validated CouponSpuDTO dto) throws Exception {
        return Result.saveOrUpdate(couponSpuService.saveOrUpdate(dto));
    }

    /**
     * 功能描述：根据ID获取优惠券与产品关联
     *
     * @param request 请求
     * @param couponSpuId  优惠券与产品关联ID
     * @return 结果
     */
    @ResponseBody
    @PostMapping("/info")
    public Result<CouponSpuVO> info(HttpServletRequest request, @RequestParam(name = "couponSpuId") Long couponSpuId) throws Exception {
        return Result.ok(couponSpuService.selectById(couponSpuId));
    }

    /**
     * 功能描述：删除优惠券与产品关联
     *
     * @param request 请求
     * @param couponSpuIds 批量删除的优惠券与产品关联ID
     * @return 结果
     */
    @ResponseBody
    @PostMapping("/delete")
    public Result<Object> delete(HttpServletRequest request, @RequestBody List<Long> couponSpuIds) {
        return Result.delete(couponSpuService.delete(couponSpuIds));
    }

}
